var refresh = 0;
var menuid = 0;
function evet(){
	/*$(".tree_div").mouseover(function(){
		
		$("#tree_bg").show();
		var offset = $(this).offset();
		var offset3 = $("#tree_box").offset();
		var y = offset.top - offset3.top+2;
		$("#tree_bg").css({"top":y+'px',"*top":(y+2)+'px'});
	});
	$(".tree_div").mouseout(function(){$("#tree_bg").hide();});*/
	/*$(".tree_div").click(function(){
		$("#tree_bj").css("height","0px");
		$("#tree_bg").hide();
		var offset = $(this).offset();
		var offset3 = $("#tree_box").offset();
		var y = offset.top - offset3.top+2;
		$("#tree_click").show();
		$("#tree_click").css("top",y+"px");
	});*/
}

$(document).ready(function(){
	$(".menu").click(function(){
		$("#tree_click").hide();
		$("#tree_bg").hide();
		$("#menu_name").html($(this).attr('alt'));
		$(".menu").removeClass("selected");
		$(this).addClass("selected");
	});
});


/*
 获取栏目树形列表
 param islen 栏目深度
*/
function get_menu(id,obj,islen){
	if(islen==0)
	{
		$("#tree_box").css('top', 0);
		
		// 当前点击的栏目样式隐藏
		$("#tree_click").css('display','none');
		menuid = id;
	}
	
	// 加载中样式
	$("#"+obj).html('<img src="'+site_url+'tree_loading.gif" width="16" height="16" border="0" />');
	
	
	var touimg_src = '';
	var img_src = '';
	var mleft = 's';
	var regexp = /(.*)elbow-end-plus/;
	
	if ($("#tree_"+id).css("display")=='none'){
		if (regexp.test($("#touimg_"+id).attr("src")))
		{
			touimg_src = 'tree_elbow_end_minus.gif';
			mleft = 'ss';
		}else{
			touimg_src = 'tree_elbow_minus.gif';
		}
		img_src = "tree_folder_open.gif";
		$("#tree_"+id).show();
	}else{
		if (regexp.test($("#touimg_"+id).attr("src")))
		{
			touimg_src = 'tree_elbow_end_plus.gif';
		}else{
			touimg_src = 'tree_elbow_plus.gif';
		}
		img_src = "tree_folder.gif";
		$("#tree_"+id).hide();
	}
	$("#touimg_"+id).attr('src',site_url+touimg_src);
	$("#img_"+id).attr('src',site_url+img_src);
	
	var cache_refresh = refresh ? 'false': 'true';
	
	
	//if( id == 2 || id == 99 || id == 3 || id == 250){
//		// 控制面板
//		if(id == 2){ var $url = site_url+'tree_panel.json'; }

//		// -- 个人信息
//		if(id == 100){var $url = site_url+'tree_panel_user.json';}
//		

//		
//		// 直接加载本地JSON数据
//		$.ajax({type:'get', url:$url, cache:cache_refresh,dataType:'json', success:function(json){		   
//		 	parsejson(json);
//			
//		}});		
//	}
//	else{
		
		// 加载PHP地址，获取执行后的JSON数据
		$.ajax({type:'get', url:'?module=menu&&action=get_menu_list&menuid='+id, cache:cache_refresh,dataType:'json', success:function(json){
			 parsejson(json);
			
		}});
	//}
	
	
	// 获取json数据，并解析
	var parsejson = function(json){
		var htmls="";
		var isend ="tree_line";
		var open = new Array();
		var openmenuids = ['120','130','300','360'];//默认要展开的menuid
		$.each(json,function(i,n){
			if (json.max != null && n.name != undefined)
			{
				var click,img,touimg='';
				if(n.isfolder==1){
					if($.inArray(n.menuid,openmenuids)>=0)
					{
						open[i] = n.menuid;
					}
					img = "tree_folder.gif";
					click = " onclick=\"get_menu("+n.menuid+",'tree_"+n.menuid+"',"+(islen+1)+"); \"";
					
					if(n.url !== '') click += " href=\""+n.url+"\" target=\"right\" ";
					
					if(n.menuid == json.max){
						touimg = "tree_elbow_end_plus.gif";
						isend = "end";
					}else{
						touimg = "tree_elbow_plus.gif";
					}
				}else{
					img = "tree_leaf.gif";
					
					click = " href=\""+n.url+"\" target=\"right\"  ";
					
					if (n.menuid == json.max)
					{
						touimg = 'tree_elbow_end.gif';
					}else{
						touimg = 'tree_elbow.gif';
					}
				}
				htmls += "<a class='tree_div' "+ click +"  id='tree_div_"+n.menuid+"'>";
				var width = islen*16;
				htmls += '<span class="tree_img"><img src="'+site_url+touimg+'" id="touimg_'+n.menuid+'" width="16" height="18" border="0" /><img src="'+site_url+img+'" id="img_'+n.menuid+'" width="16" height="16" border="0" /></span><span class="tree_text">'+n.name+'</span></a>';
				if(n.isfolder==1){htmls +='<div id="tree_'+n.menuid+'" class="'+isend+'" style="display:none;"></div>';}
			}
		});
		if (htmls)
		{
			$("#"+obj).html(htmls);
		}else{
			$("#tree_"+id).hide();
		}
		if(open)
		{
		$.each(open,function(i,n){get_menu(n,'tree_'+n,1)});
		}
		evet();
	 }
	
	
}

function menu_refresh()
{
	if (refresh==1)
	{
		refresh = 0;
		$('#menurefresh').attr('title', '点击设置为刷新状态');
		$('#menurefresh').attr('alt', '点击设置为刷新状态');
		$('#menurefresh').attr('src', site_url+'tree_refresh.gif');
	}
	else
	{
		refresh = 1;
		$('#menurefresh').attr('title', '点击设置为缓存状态');
		$('#menurefresh').attr('alt', '点击设置为缓存状态');
		$('#menurefresh').attr('src', site_url+'tree_refreshed.gif');		
		get_menu(menuid, 'tree', 0);
		setTimeout(menu_refresh, 30000);	
	}
}